<template>
  <div>
    <div class="gui-padding bgf">
      <div class="mb15">
        <a-breadcrumb>
          <a-breadcrumb-item>船员管理</a-breadcrumb-item>
          <a-breadcrumb-item>离船申请</a-breadcrumb-item>
        </a-breadcrumb>
      </div>
      <div class="mb10 flex align-items justify-content-between">
        <div>
          <span class="mr5"><a-button type="primary" size="small" :loading="addLoading" @click="handleOpenAdd"> <template #icon><PlusOutlined/> </template>新增</a-button></span>
        </div>
        <a-form layout="inline"
                autocomplete="off">
          <a-form-item label="申请日期">
            <a-range-picker v-model:value="dateArr" value-format="YYYY-MM-DD" format="YYYY-MM-DD"/>
          </a-form-item>
          <a-form-item label="船员姓名">
            <a-input v-model:value="queryParams.keyword" size="small" @change="handleSearch" style="width: 160px;height: 32px" placeholder="请输入船员姓名&离船原因" allow-clear/>
          </a-form-item>
          <a-form-item >
            <a-button type="primary" @click.prevent="handleSearch" size="small">
              <SearchOutlined/>搜索
            </a-button>
          </a-form-item>
<!--          <a-form-item >
            <a-button type="primary" ghost size="small">
              重置
            </a-button>
          </a-form-item>-->
        </a-form>
      </div>
      <a-table :columns="columns"
               rowClassName="items"
               :loading="false"
               :scroll="{ x: 'calc(100vw - 326px)', y: 'calc(100vh - 268px)' }"
               :pagination="false"
               :data-source="tableData"
               size="small" sticky bordered>
        <template #bodyCell="{ column, text, record }">
          <template v-if="column.dataIndex === 'shenPiZhuangTai'">
            <div class="editable-row-operations pointer" @click="handleShowEdit(record.id)">
                {{record.shenPiZhuangTai}}
            </div>
          </template>
          <template v-if="column.dataIndex === 'operation'">
            <a-button type="primary" size="small" @click="handleShowEdit(record.id)">审批</a-button>
          </template>
        </template>
      </a-table>
      <div class="flex  mt20">
        <a-pagination
            v-model:current="queryParams.pageNo"
            v-model:page-size="queryParams.pageSize"
            @change="handleChangePageNo"
            :page-size-options="pageSizeOptions"
            :show-total="total => `共 ${total} 项`"
            show-size-changer
            :total="total"/>
      </div>
      <add-application ref="addRef" @success="handleLoadData"></add-application>
      <add-application-detail ref="showRef" @success="handleLoadData"></add-application-detail>
    </div>
  </div>
</template>
<script lang="ts" setup>
import AddApplication from "./compoments/AddApplication.vue";
import AddApplicationDetail from "./compoments/AddApplicationDetail.vue";

const dateArr = ref([])
const tableData = ref([]);
const total = ref(0)
const pages = ref(0)
const addLoading = ref(false)
const tableLoading = ref(false)
const addRef = ref({})
const showRef = ref({})
const pageSizeOptions = ref(['10', '20', '30', '40', '50'])
const queryParams = reactive({
  "pageNo": 1,
  "pageSize": 20,
  "keyword": "",
  "startDate": "",
  "endDate": ""
})

// 获取选择的行id
const state = reactive({
  selectedRowKeys: [],
  loading: false,
})

// 定义表格的列信息
const columns = [
  {
    title: '序号',
    fixed: "left",
    align:"center",
    width: 60,
    dataIndex: 'index',
    customRender: ({text, record, index}) =>  index + 1
  },
  {
    title: '离船申请单号',
    align:"center",
    dataIndex: 'shenQingDanHao',
    width: '180px',
  },
  {
    title: '所在船舶',
    align:"center",
    dataIndex: 'suoZaiChuanBo',
    width: '100px',
  },
  {
    title: '职位',
    dataIndex: 'zhiWei',
    width: '100px',
  },
  {
    title: '船员姓名',
    dataIndex: 'chuanYuanMing',
    width: '100px',
  },
  {
    title: '上船日期',
    dataIndex: 'shangChuanRiQi',
    width: '100px',
  },
  {
    title: '计划下船开始日期',
    dataIndex: 'xiaChuanKaiShiRiQi',
    width: '100px',
  },
  {
    title: '计划下船结束日期',
    dataIndex: 'xiaChuanJieShuRiQi',
    width: '100px',
  },
  {
    title: '申请日期',
    align: 'center',
    dataIndex: 'shengQingRiQi',
    width: '120px',
  },
  {
    title: '申请状态',
    align: 'center',
    dataIndex: 'shenPiZhuangTai',
    width: '120px',
  },
  {

    title: '操作',
    dataIndex: 'operation',
    align: "center",
    fixed: "right",
    width: '160px',
  }
];

// 查询表格信息
const handleLoadData = async ()=>{
  if (dateArr.value && dateArr.value.length > 0) {
    queryParams.startDate = dateArr.value[0];
    queryParams.endDate = dateArr.value[1];
  } else {
    queryParams.startDate = "";
    queryParams.endDate = "";
  }
  tableLoading.value = true;
  const res = await useLoadLiChuanShenQingApi(queryParams)
  tableLoading.value = false
  tableData.value = res.data.records
  total.value = res.data.total
}

// 搜索
const handleSearch = ()=>{
  queryParams.pageNo = 1;
  handleLoadData()
}

const handleChangeUpdate = async(record,field)=>{
  var params = {
    id:record.id,
    field:field,
    value:record[field]
  }
  await useUpdateStatusShops(params)
  useMessage().success("操作成功!")
}




const handleShowEdit = (id) => {
  showRef.value.handleOpen(id)
}


// 改变分页
const handleChangePageNo = (pno, psize) => {
  queryParams.pageNo = pno;
  queryParams.pageSize = psize;
  handleLoadData()
}


const handleOpenAdd = ()=>{
  addLoading.value = true
  addRef.value.handleOpen()
  addLoading.value = false
}

const handleEdit = (id) => {
  addLoading.value = true
  addRef.value.handleOpen(id)
  addLoading.value = false
}

const handleDelete = async(id) => {
  const res= await useDeleteShops(id)
  useMessage().success("删除成功!");
  handleLoadData()
}

onMounted(()=>{
  handleLoadData()
})
</script>
<style scoped>
.editable-row-operations a {
  margin-right: 8px;
}
</style>
